package com.hotel.writer.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;

import java.io.File;
import java.io.IOException;

public class LuceneWriter {
    public static void main(String[] args) throws IOException, ParseException {
        File file = new File("D:/opt/ejob/lucene/index/position");
        Directory indexDir = new SimpleFSDirectory(file);
        IndexSearcher searcher = null;
        searcher = new IndexSearcher(DirectoryReader.open(indexDir));
        BooleanQuery query = new BooleanQuery();
        BooleanClause clause = new BooleanClause(new TermQuery(new Term("positionName", "工程师")), BooleanClause.Occur.MUST);
        BooleanClause _clause = new BooleanClause(new TermQuery(new Term("workingLife", "THREEABOVE")), BooleanClause.Occur.MUST);
        query.add(clause);
        query.add(_clause);
        System.out.println(query);
        TopDocs hits = searcher.search(query, 20);
        System.out.println(hits.totalHits);
        for (int i = 0; i < hits.scoreDocs.length; i++) {
            int docId = hits.scoreDocs[i].doc;
            Document hit = searcher.doc(docId);
            System.out.println(hit.get("positionId") + "|"
                    + hit.get("positionName") + "|"
                    + hit.get("positionType") + "|"
                    + hit.get("workingLife") + "|"
                    + hit.get("recruitNum") + "|"
                    + hit.get("companyId") + "|"
                    + hit.get("companyName") + "|"
                    + hit.get("companyScore") + "|"
                    + hit.get("companyNature") + "|"
                    + hit.get("companySize") + "|"
                    + hit.get("salaryLow") + "|"
                    + hit.get("salaryHight") + "|"
                    + hit.get("departmentId") + "|"
                    + hit.get("departmentName") + "|"
                    + hit.get("cityName") + "|"
                    + hit.get("educationRequirements"));
        }

    }
}
